package com.fl.nj;

import com.fl.nj.config.DBConfig;
import com.fl.nj.pool.ConnectionPoolManager;
import com.fl.nj.rule.DataFetchRule;
import com.fl.nj.util.DBUtil;
import com.fl.nj.util.FileUtil;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * 测试类
 * 用于测试各个组件的功能
 */
public class TestMain {
    private static final String TEST_DB_POOL_ID = "test_db";
    private static final String TEST_FILE_PATH = "/tmp/test_data.json";

    public static void main(String[] args) {
        try {
            System.out.println("===== 开始测试数据处理组件 =====");

            // 1. 测试数据库连接池
            testConnectionPool();

            // 2. 测试数据获取规则
            testDataFetchRule();

            // 3. 测试数据导出到文件
            testExportDataToFile();

            // 4. 测试从文件导入数据
            testImportDataFromFile();

            System.out.println("===== 所有测试完成 =====");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            ConnectionPoolManager.closeAllDataSources();
        }
    }

    /**
     * 测试数据库连接池
     */
    private static void testConnectionPool() throws SQLException {
        System.out.println("\n测试数据库连接池...");
        // 初始化数据库连接池
        DBConfig dbConfig = new DBConfig();
        dbConfig.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai");
        dbConfig.setUsername("root");
        dbConfig.setPassword("123456");
        dbConfig.setDriverClassName("com.mysql.cj.jdbc.Driver");

        ConnectionPoolManager.initDataSource(TEST_DB_POOL_ID, dbConfig);

        // 获取数据库连接
        Connection conn = ConnectionPoolManager.getConnection(TEST_DB_POOL_ID);
        System.out.println("成功获取数据库连接: " + (conn != null && !conn.isClosed()));
        conn.close();
    }

    /**
     * 测试数据获取规则
     */
    private static void testDataFetchRule() throws SQLException {
        System.out.println("\n测试数据获取规则...");
        // 创建数据获取规则
        DataFetchRule rule = new DataFetchRule();
        rule.setTableName("user");
        rule.setWhereClause("1=1"); // 简单条件
        rule.setLimit(5); // 只获取5条记录

        System.out.println("生成的SQL: " + rule.buildSql());

        // 执行查询
        List<Map<String, Object>> result = DBUtil.fetchDataByRule(TEST_DB_POOL_ID, rule);
        System.out.println("查询结果数量: " + result.size());
        if (!result.isEmpty()) {
            System.out.println("第一条记录: " + result.get(0));
        }
    }

    /**
     * 测试数据导出到文件
     */
    private static void testExportDataToFile() throws Exception {
        System.out.println("\n测试数据导出到文件...");
        // 获取测试数据
        DataFetchRule rule = new DataFetchRule();
        rule.setTableName("user");
        rule.setLimit(10);

        List<Map<String, Object>> dataList = DBUtil.fetchDataByRule(TEST_DB_POOL_ID, rule);
        System.out.println("准备导出" + dataList.size() + "条记录到文件");

        // 导出数据到JSON文件
        FileUtil.saveDataToJsonFile(TEST_FILE_PATH, dataList);
        System.out.println("数据已导出到文件: " + TEST_FILE_PATH);
    }

    /**
     * 测试从文件导入数据
     */
    private static void testImportDataFromFile() throws Exception {
        System.out.println("\n测试从文件导入数据...");
        // 从文件读取数据
        List<Map<String, Object>> dataList = FileUtil.readDataFromJsonFile(TEST_FILE_PATH);
        System.out.println("从文件读取到" + dataList.size() + "条记录");

        if (!dataList.isEmpty()) {
            System.out.println("读取的第一条记录: " + dataList.get(0));
        }
    }
}